﻿/****************************** Module Header ******************************\
* Plus Name:  JqScrollUp.js
* Plus Version:  1.0.0 (2012-7-16)
* Copyright (c) JianDanMai.Com (14507247@qq.com)
* 
* This plus used to option the scrolling.
* 
* This source is subject to the GPL License.
* See http://www.gnu.org/licenses/gpl.html.
* All other rights reserved.
* 
* THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 
* EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED 
* WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
\***************************************************************************/

(function ($) {
    $.fn.extend({
        "JqScrollUp": function (options) {

            /// 参数:s 滚动速度
            /// 参数:d 每次滚屏高度
            /// 参数:c 当前已滚动高度
            /// 参数:t 滚动时间间隔
            options = $.extend({ d: 90, c: 0, s: 9, t: 4000 }, options);

            // 事件滚动
            var o = $(this)[0];
            window.setInterval(function () { scrollup(o, options.d, options.c) }, options.t);

            ///滚动主方法
            ///参数:o 滚动块对象
            ///参数:d 每次滚屏高度
            ///参数:c 当前已滚动高度
            function scrollup(o, d, c) {
                if (d == c) {
                    var t = getFirstChild(o.firstChild).cloneNode(true);
                    o.removeChild(getFirstChild(o.firstChild));
                    o.appendChild(t);
                    t.style.marginTop = "0px";
                } else {
                    c += options.s;
                    getFirstChild(o.firstChild).style.marginTop = -c + "px";
                    window.setTimeout(function () { scrollup(o, d, c) }, 10);
                }
            }
            //解决firefox下会将空格回车作为节点的问题
            function getFirstChild(node) {
                while (node.nodeType != 1) {
                    node = node.nextSibling;
                }
                return node;
            }
        }
    });
})(jQuery);